package com.sav.common.catalog;

import java.util.ArrayList;
import java.util.List;

import com.sav.common.containers.ProductContainer;

public class Cart {
	private int idUser;
	private ArrayList<ProductContainer> listProducts = new ArrayList<ProductContainer>();
	
	public Cart() {
		
	}
	
	public Cart(int idUser) {
		this.idUser = idUser;
	}
	
	public int getIdUser() {
		return idUser;
	}

	public void setIdUser(int idUser) {
		this.idUser = idUser;
	}
	
	public void addProduct(int idProduct, int number) {
		listProducts.add(new ProductContainer(idProduct, number));
	}
	
	public void setNumberProduct(int idProduct, int number) {
		for (ProductContainer l : listProducts) {
			if (l.getIdProduct() == idProduct) l.setNumber(number);
		}
	}
	
	public void removeProduct(int idProduct) {
		for (ProductContainer l : listProducts) {
			if (l.getIdProduct() == idProduct) {
				listProducts.remove(l);
			}
		}
	}
	
	public List<ProductContainer> getListProducts() {
		return listProducts;
	}
	
	public void clearCart() {
		listProducts.clear();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + idUser;
		result = prime * result
				+ ((listProducts == null) ? 0 : listProducts.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Cart other = (Cart) obj;
		if (idUser != other.idUser)
			return false;
		if (listProducts == null) {
			if (other.listProducts != null)
				return false;
		} else if (!listProducts.equals(other.listProducts))
			return false;
		return true;
	}
	
}
